package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @Classname: Demo12
 * @Author: bromide
 * @CreateTime: 2022/5/10--14:42
 * @Version: V1.0
 * @Description:
 */
public class Demo12 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用户名:");
        String username = scanner.nextLine();
        System.out.print("请输入密码:");
        String password = scanner.nextLine();
        System.out.print("请输入昵称:");
        String nick = scanner.nextLine();
        try(Connection conn = DBUtils.getConn()) {
            PreparedStatement ps = conn.prepareStatement("select password from user where username=?");
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                //有下一个  说明有用户名
                System.out.println("用户名存在!");
            }else {
                ps = conn.prepareStatement("insert into user values(null,?,?,?)");
                ps.setString(1,username);
                ps.setString(2,password);
                ps.setString(3,nick);
                ps.executeUpdate();
                System.out.println("注册成功!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
